<!-- 角色编辑弹窗 -->
<template>
  <a-modal
    :width="460"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate?'修改角色':'添加角色'"
    :body-style="{paddingBottom: '8px'}"
    @update:visible="updateVisible"
    @ok="save">
    <a-form
      ref="form"
      :model="form"
      :rules="rules"
      :label-col="{md: {span: 5}, sm: {span: 24}}"
      :wrapper-col="{md: {span: 19}, sm: {span: 24}}">
      <a-form-item label="角色名称:" name="roleName">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="请输入角色名称"
          v-model:value="form.roleName"/>
      </a-form-item>
      <a-form-item label="角色标识:" name="roleCode">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="请输入角色标识"
          v-model:value="form.roleCode"/>
      </a-form-item>
      <a-form-item label="登陆系统:" name="loginSystems">
        <m-dict-select
          dict="系统类型"
          mode="multiple"
          @change="changeLoginSystem"
          v-model:value="form.loginSystems"
          placeholder="请选择允许登陆系统"
        >
        </m-dict-select>
      </a-form-item>
      <a-form-item label="备注:">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入备注"
          v-model:value="form.comments"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import MDictSelect from "@/components/MDict/dictSelect";
export default {
  name: 'RoleEdit',
  components: {MDictSelect},
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        roleName: [
          {required: true, message: '请输入角色名称', type: 'string', trigger: 'blur'}
        ],
        roleCode: [
          {required: true, message: '请输入角色标识', type: 'string', trigger: 'blur'}
        ],
        loginSystems:[
          {required: true, message: '请选择角色允许登陆的系统',type:"array", trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.form.loginSystems=this.data.loginSystems?this.data.loginSystems.split(","):[]
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
      if (this.$refs.form) {
        this.$refs.form.clearValidate();
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs.form.validate().then(() => {
        this.loading = true;
        let data=JSON.parse(JSON.stringify(this.form))
        data.loginSystems=data.loginSystems.join(",")
        this.$http[this.isUpdate ? 'put' : 'post']('/sys/role', data).then(res => {
          this.loading = false;
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            if (!this.isUpdate) {
              this.form = {};
            }
            this.updateVisible(false);
            this.$emit('done');
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          this.loading = false;
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    changeLoginSystem(e){
      console.log("e",e);
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
